package com.qf.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.CarServiceMapper;
import com.qf.ssm.qo.CarMaintenanceQO;
import com.qf.ssm.qo.CarMaintenanceReturnQO;
import com.qf.ssm.qo.CarRemindQo;
import com.qf.ssm.qo.CarServiceQO;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CarService;
import com.qf.ssm.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarServiceMapper carServiceMapper;

    @Override
    public PageInfo<CarInsuranceVO> queryCarInsuranceServiceList(int page, int size, CarServiceQO carServiceQO) {
        String storeName = carServiceQO.getStoreNameKeyWord();
        if ("全部".equals(storeName)) {
            carServiceQO.setStoreNameKeyWord(null);
        }
        PageInfo<CarInsuranceVO> resultList = null;
        resultList = PageHelper.startPage(page, size).doSelectPageInfo(() -> carServiceMapper.selectCarInsuranceServiceList(carServiceQO));
        if (resultList == null) {
            throw new ServiceException(ResultStatus.GET_DATA_ERROR);
        }
        return resultList;
    }

    @Override
    public PageInfo<CarAnnualInspectionVo> queryCarAnnualInspectionServiceList(int page, int size, CarServiceQO carServiceQO) {
        String storeName = carServiceQO.getStoreNameKeyWord();
        if ("全部".equals(storeName)) {
            carServiceQO.setStoreNameKeyWord(null);
        }
        PageInfo<CarAnnualInspectionVo> resultList = null;
        resultList = PageHelper.startPage(page, size).doSelectPageInfo(() -> carServiceMapper.selectCarAnnualInspectionServiceList(carServiceQO));
        if (resultList == null) {
            throw new ServiceException(ResultStatus.GET_DATA_ERROR);
        }
        return resultList;
    }

    @Override
    public PageInfo<CarMaintenanceVO> queryCarMaintenanceServiceList(int page, int size, CarServiceQO carServiceQO) {
        String storeName = carServiceQO.getStoreNameKeyWord();
        if ("全部".equals(storeName)) {
            carServiceQO.setStoreNameKeyWord(null);
        }
        PageInfo<CarMaintenanceVO> resultList = null;
        resultList = PageHelper.startPage(page, size).doSelectPageInfo(() -> carServiceMapper.selectCarMaintenanceServiceList(carServiceQO));
        if (resultList == null) {
            throw new ServiceException(ResultStatus.GET_DATA_ERROR);
        }
        return resultList;
    }

    @Override
    public Integer modifyCarInsuranceInfo(CarInsuranceVO carInsuranceVO) {
        int count = carServiceMapper.updateCarInsuranceVO(carInsuranceVO);
        if(count<1){
            throw new ServiceException(ResultStatus.UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public Integer modifyCarAnnualInspectionInfo(CarAnnualInspectionVo carAnnualInspectionVo) {
        int count = carServiceMapper.updateCarAnnualInspectionVo(carAnnualInspectionVo);
        if(count<1){
            throw new ServiceException(ResultStatus.UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public Integer modifyCarMaintenanceInfo(CarMaintenanceVO carMaintenanceVO) {
        int count = carServiceMapper.updateCarMaintenanceVO(carMaintenanceVO);
        if(count<1){
            throw new ServiceException(ResultStatus.UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public Integer saveCarMaintenance(CarMaintenanceQO carMaintenanceQO) {
        int count = carServiceMapper.insertCarMaintenance(carMaintenanceQO);
        if(count<1){
            throw new ServiceException(ResultStatus.SQL_INSERT_ERROR);
        }
        return count;
    }

    @Override
    public Integer modifyCarMaintenanceReturn(CarMaintenanceReturnQO carMaintenanceReturnQO) {
        int count = carServiceMapper.updateCarMaintenanceReturn(carMaintenanceReturnQO);
        if(count<1){
            throw new ServiceException(ResultStatus.SYS_ERROR);
        }
        return count;
    }

    @Override
    public CarMaintenanceDetailVO selectCarMaintenanceDetail(int carId) {
        CarMaintenanceDetailVO result = carServiceMapper.selectCarMaintenanceDetail(carId);
        if(result==null){
            throw new ServiceException(ResultStatus.GET_DATA_ERROR);
        }
        return result;
    }

    @Override
    public CarRemindTimeVo remind(CarRemindQo carRemindQo) {
        CarRemindTimeVo carRemindTimeVo = new CarRemindTimeVo();
        if (carRemindQo.getPlateNumber() != null && carRemindQo.getPlateNumber() != ""){
            CarRemindVo carRemindVo = carServiceMapper.selectCarRemindVo(carRemindQo.getPlateNumber());
            String insurance = carRemindVo.getInsurance();
            String inspection = carRemindVo.getInspection();
            String maintainFinishTime = carRemindVo.getMaintainFinishTime();

            System.out.println(insurance+","+inspection+","+maintainFinishTime);

            LocalDate dateInsurance = LocalDate.parse(insurance, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            LocalDate dateInspection = LocalDate.parse(inspection, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            LocalDate dateMaintainFinishTime = LocalDate.parse(maintainFinishTime.substring(0, 10), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            //计算车险提醒日期
            LocalDate insuranceRemindDate = dateInsurance.minusDays(carRemindQo.getRemindInsurance());
            //计算车检提醒日期
            LocalDate inspectionRemindDate = dateInspection.minusDays(carRemindQo.getRemindInspection());
            //计算剩余公里数后去保养
            Integer i = carRemindQo.getKilometres() - carRemindVo.getKilometres();
            //计算剩余年数后去保养
            LocalDate localDate = dateMaintainFinishTime.plusDays(carRemindQo.getRemindMaintain());
            carRemindTimeVo.setInsuranceRemindDate(insuranceRemindDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            carRemindTimeVo.setInspectionRemindDate(inspectionRemindDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            carRemindTimeVo.setRemindKilometres(i);
            carRemindTimeVo.setRemindDateMaintainFinishTime(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            return carRemindTimeVo;
        }
        return null;
    }
}
